﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class RotationRuleBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly IRotationRuleDA rotationRuleDA = DataAccess.CreateRotationRuleDA();
        private static readonly IRotationRuleDetailDA rotationRuleDetailDA = DataAccess.CreateRotationRuleDetailDA();

        public RotationRuleBLL()
        {
            this.pubParasInfo = null;
        }

        public RotationRuleBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public RotationRuleBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<RotationRuleInfo> rotationRuleInfos = new List<RotationRuleInfo>();
                RotationRuleInfo rotationRuleInfo = null;
                foreach (string id in IDS)
                {
                    rotationRuleInfo = rotationRuleDA.GetRotationRuleByID(dataBase, tran, id, this.dataBaseID[0]);
                    rotationRuleInfo.RotationRuleDetailInfo = rotationRuleDetailDA.GetRotationRuleDetailListByID(dataBase, tran, id, this.dataBaseID[0]);
                    rotationRuleInfos.Add(rotationRuleInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                rotationRuleDetailDA.DeleteRotationRuleDetail(dataBase, tran, IDS, this.dataBaseID);
                rotationRuleDA.DeleteRotationRule(dataBase, tran, IDS, this.dataBaseID);
                for (int i = 0; i < IDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<RotationRuleInfo>(dataBase, tran, rotationRuleInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ROTATION_RULE", this.dataBaseID[0]);
                    if ((rotationRuleInfos[i].RotationRuleDetailInfo != null) && (rotationRuleInfos[i].RotationRuleDetailInfo.Count > 0))
                    {
                        for (int j = 0; j < rotationRuleInfos[i].RotationRuleDetailInfo.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<RotationRuleDetailInfo>(dataBase, tran, rotationRuleInfos[i].RotationRuleDetailInfo[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "ROTATION_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + IDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return rotationRuleDA.GetAllRotationRule(this.dataBaseID[0]);
        }

        public object GetInfoByID(string rotationRuleID)
        {
            RotationRuleInfo rotationRule = rotationRuleDA.GetRotationRuleByID(rotationRuleID, this.dataBaseID[0]);
            if (rotationRule != null)
            {
                rotationRule.RotationRuleDetailInfo = rotationRuleDetailDA.GetRotationRuleDetailListByID(rotationRuleID, this.dataBaseID[0]);
            }
            return rotationRule;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            RotationRuleQueryEntity rotationRule = obj as RotationRuleQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(rotationRule.RotationRuleID))
            {
                sqlWhere.Add(" ROTATION_RULE_ID " + rotationRule.RotationRuleID);
            }
            if (!string.IsNullOrEmpty(rotationRule.Descr))
            {
                sqlWhere.Add(" DESCR " + rotationRule.Descr);
            }
            if (!string.IsNullOrEmpty(rotationRule.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + rotationRule.IsActive);
            }
            return rotationRuleDA.GetRotationRuleByQueryList(sqlWhere, rotationRule, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string rotationRuleID = string.Empty;
            try
            {
                RotationRuleInfo rotationRule = obj as RotationRuleInfo;
                if (rotationRuleDA.CheckRotationRuleIDUnique(dataBase, tran, rotationRule.RotationRuleID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2420_001", this.pubParasInfo.UserLanguage, new List<string> { rotationRule.RotationRuleID });
                }
                rotationRule.UpdatedBy = rotationRule.CreatedBy;
                rotationRule.CreatedDate = DateTime.Now;
                rotationRule.UpdatedDate = DateTime.Now;
                rotationRuleDA.InsertRotationRule(dataBase, tran, rotationRule, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<RotationRuleInfo>(dataBase, tran, null, rotationRule, AuditAction.Insert, this.pubParasInfo.UserID, "ROTATION_RULE", this.dataBaseID[0]);
                rotationRuleID = rotationRule.RotationRuleID;
                if ((rotationRule.RotationRuleDetailInfo != null) && (rotationRule.RotationRuleDetailInfo.Count > 0))
                {
                    for (int i = 0; i < rotationRule.RotationRuleDetailInfo.Count; i++)
                    {
                        if (rotationRuleDetailDA.CheckRotationRuleDetailIDUnique(dataBase, tran, rotationRule.RotationRuleDetailInfo[i].RotationRuleID, rotationRule.RotationRuleDetailInfo[i].LineID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2420_002", this.pubParasInfo.UserLanguage, new List<string> { rotationRule.RotationRuleDetailInfo[i].RotationRuleID, rotationRule.RotationRuleDetailInfo[i].LineID.ToString() });
                        }
                        else
                        {
                            rotationRule.RotationRuleDetailInfo[i].CreatedDate = DateTime.Now;
                            rotationRule.RotationRuleDetailInfo[i].UpdatedDate = DateTime.Now;
                            rotationRule.RotationRuleDetailInfo[i].CreatedBy = this.pubParasInfo.UserID;
                            rotationRule.RotationRuleDetailInfo[i].UpdatedBy = this.pubParasInfo.UserID;
                            rotationRuleDetailDA.InsertRotationRuleDetail(dataBase, tran, rotationRule.RotationRuleDetailInfo[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<RotationRuleDetailInfo>(dataBase, tran, null, rotationRule.RotationRuleDetailInfo[i], AuditAction.Insert, this.pubParasInfo.UserID, "ROTATION_RULE_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return rotationRuleID;
        }

        public string Update(object obj)
        {
            string rotationRuleID = string.Empty;
            try
            {
                RotationRuleInfo rotationRule = obj as RotationRuleInfo;
                RotationRuleInfo oldRotationRuleInfo = rotationRuleDA.GetRotationRuleByID(rotationRule.RotationRuleID, this.dataBaseID[0]);
                if (rotationRuleDA.UpdateRotationRule(rotationRule, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<RotationRuleInfo>(oldRotationRuleInfo, rotationRule, AuditAction.Update, this.pubParasInfo.UserID, "ROTATION_RULE", this.dataBaseID[0]);
                    rotationRuleID = rotationRule.RotationRuleID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return rotationRuleID;
        }
    }
}

